<?php

namespace Home\Controller;
use Common\Controller\BaseController;

/**
 * Class EquRepairRecordController
 * @package Home\Controller
 * 设备维保记录管理
 */
class EquRepairRecordsController extends BaseController {

   public function index(){

        $repair_record_no = I('get.repair_record_no','','htmlspecialchars');
        $number = I('get.number','','htmlspecialchars');

        $equRecordsModel = D('EquRepairRecords');

        $where = " equ_repair_records.del=1 ";
        
        if(!empty($repair_record_no)){
        	$where  .= " and locate('{$repair_record_no}',equ_repair_records.repair_record_no) ";
        }

        if(!empty($number)){
        	$where  .= " and locate('{$number}',equipment.number) ";
        }

        $field = "equ_repair_records.*,user.name as uname,user.nickname,equipment.number as equnumber,equipment.name as equname,comment.score,company.name as com_name";
        $list = $equRecordsModel
            ->field($field)
            ->join('equipment on equipment.equipment_id=equ_repair_records.equipment_id','LEFT')
            ->join('comment on equ_repair_records.comment_id=comment.comment_id','LEFT')
            ->join('company on equ_repair_records.company_id=company.company_id','LEFT')
            ->join('user on equ_repair_records.user_id=user.user_id','LEFT')
            ->where($where)
            ->order('equ_repair_records_id DESC')
            ->select();
         // echo $equRecordsModel->_sql();
         // dump($list);exit;
        $this->assign('list',$list);
        $this->assign('repair_record_no',$repair_record_no);
        $this->assign('number',$number);

        $this->display('index');
    }

    public function add(){
        
        $info = $this->getInfoList();

        $this->assign('now',date('Y-m-d'));
        $this->assign('users',$info['users']);
        $this->assign('contract',$info['contract']);
        $this->assign('equipment',$info['equipment']);
        $this->assign('company',$info['company']);

        $this->display('add');
    }

    public function edit(){

        $equ_id    = I('get.equid');
        $equRecordsModel  = D('EquRepairRecords');

        $equ_repair_records = $equRecordsModel->where('equ_repair_records_id='.intval($equ_id).' and del=1')->limit(1)->select();

        if(empty($equ_repair_records)){
            $this->error('维保纪录不存在');
        }

        $info = $this->getInfoList();

        $this->assign('now',date('Y-m-d'));
        $this->assign('users',$info['users']);
        $this->assign('contract',$info['contract']);
        $this->assign('equipment',$info['equipment']);
        $this->assign('company',$info['company']);

        $this->assign('equ_repair_records',$equ_repair_records[0]);
        $this->display('edit');
    }

    public function save(){

        $ret = array('status'=>200,'msg'=>'','content'=>'');

        if(IS_POST){
            try{

                $equModel = D('EquRepairRecords');
                $post      = I('post.');
                $res = $equModel->saveEquRepairRecords($post);
                $ret['status'] = (false===$ret) ? 0 : 200;
                $ret['msg'] = $res['msg'];

            }catch (\Exception $e){

                $ret['status'] = $e->getCode();
                $ret['msg']    = $e->getMessage()." on line ".$e->getLine();
            }
        }else{
            $ret['msg'] = '请求不允许';
        }

        $this->ajaxReturn($ret);
    }
    public function delete(){

        
        $equ_id    = I('get.equid');
        $equModel  = D('EquRepairRecords');

        $equipment = $equModel->where('equ_repair_records_id='.intval($equ_id).' and del=1')->limit(1)->select();
	
        if(!empty($equipment)){
            try{
                $info = array('equ_repair_records_id'=>intval($equ_id),'del'=>2);
                $res = $equModel->setField($info);
                if(false===$res){
                    $ret['status'] = 0;
                    $ret['msg']    = "删除失败";
                }else{
                    $ret['msg']    = "删除成功";
                }

            }catch (\Exception $e){

                $ret['status'] = $e->getCode();
                $ret['msg']    = $e->getMessage()." on line ".$e->getLine();
            }
        }else{
            $ret['msg'] = '维保纪录不存在';
        }
        $this->ajaxReturn($ret);
    }

    public function view(){
        $equ_id    = I('get.equid');
        $equRecordsModel  = D('EquRepairRecords');
        
        $field = "equ_repair_records.*,user.name as uname,user.nickname,equipment.number as equnumber,equipment.name as equname,comment.score,company.name as com_name";
        $equ_repair_records = $equRecordsModel
            ->field($field)
            ->join('equipment on equipment.equipment_id=equ_repair_records.equipment_id','LEFT')
            ->join('comment on equ_repair_records.comment_id=comment.comment_id','LEFT')
            ->join('company on equ_repair_records.company_id=company.company_id','LEFT')
            ->join('user on equ_repair_records.user_id=user.user_id','LEFT')
            ->where("equ_repair_records_id={$equ_id} and equ_repair_records.del=1")
            ->limit(1)
            ->select();

        if(empty($equ_repair_records)){
            $this->error('维保纪录不存在');
        }

        $this->assign('equ_repair_records',$equ_repair_records[0]);
        $this->display('view');
    }
    public function getInfoList(){
        //获取维保人员列表
        $userModel = D('User');
        $users    = $userModel->getUserList($userModel::$ROLE_WEIBAO);
        //获取合同列表
        $contractModel = D('Contract');
        $contract = $contractModel->getContractList();
        //获取设备列表
        $equModel = D('Equipment');
        $equipment = $equModel->getEquList();

        //获取企业信息
        $companyModel = D('Company');
        $company      = $companyModel->getCompanyList();

        $return = array(
            'users'     =>$users,
            'contract'  =>$contract,
            'equipment' =>$equipment,
            'company'   =>$company
        );
        return $return;
    }
}